package org.example.myleet.p859;

public class Solution {
    public boolean buddyStrings(String s, String goal) {
        int m = s.length(), n = goal.length();
        if (m != n) {
            return false;
        }
        int[] sCharCount = new int[26];
        int[] goalCharCount = new int[26];
        int diffPosCount = 0;
        for (int i = 0; i < m; ++i) {
            char sc = s.charAt(i);
            char gc = goal.charAt(i);
            ++sCharCount[sc - 'a'];
            ++goalCharCount[gc - 'a'];
            if (sc != gc) {
                ++diffPosCount;
            }
        }
        for (int i = 0; i < 26; ++i) {
            if (sCharCount[i] != goalCharCount[i]) {
                return false;
            }
        }
        if (diffPosCount == 0) {
            for (int i = 0; i < 26; ++i) {
                if (sCharCount[i] > 1) {
                    return true;
                }
            }
            return false;
        } else return diffPosCount == 2;
    }
}
